package com.itheima.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.domain.YddAdvimg;
import com.itheima.youdianda.domain.YddAdvpos;
import com.itheima.youdianda.dto.PageDto;
import com.itheima.youdianda.dto.YddAdvposDto;
import com.itheima.youdianda.exception.CustomException;
import com.itheima.youdianda.mapper.YddAdvposMapper;
import com.itheima.youdianda.service.YddAdvimgService;
import com.itheima.youdianda.service.YddAdvposService;
import com.itheima.youdianda.utils.JwtUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName YddAdvposServiceImpl
 * @Description
 * @Author Young
 * @Date 2023/2/13 1:30
 * @Version 1.0
 */
@Service
public class YddAdvposServiceImpl extends ServiceImpl<YddAdvposMapper,YddAdvpos> implements YddAdvposService {

    @Autowired
    private YddAdvposService yddAdvposService;

    @Autowired
    private YddAdvimgService yddAdvimgService;

    @Override
    public List<YddAdvpos> findall() {
        return list();
    }

    /**
     * 根据id删除广告位，删除之前需要进行判断
     * @param
     */
    @Override
    public void remove(String id) {
        LambdaQueryWrapper<YddAdvimg> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(YddAdvimg::getAdvimgpos,id);
        int count = yddAdvimgService.count(dishLambdaQueryWrapper);
        if(count > 0){
            //已经关联广告图，抛出一个业务异常
            throw new CustomException("当前广告位下此存在广告图，不能删除");
        }
        super.removeById(id);
    }

    public PageDto<YddAdvposDto> pagede( int page, int limit, String advposname, String advposdesc){
            Page<YddAdvpos> pageInfo = new Page<>(page,limit);
            Page<YddAdvposDto> yddAdvposDtoPage = new Page<>();
            LambdaQueryWrapper<YddAdvpos> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(advposname!= null,YddAdvpos::getAdvposname,advposname);
            queryWrapper.orderByDesc(YddAdvpos::getId);
            yddAdvposService.page(pageInfo,queryWrapper);
            BeanUtils.copyProperties(pageInfo,yddAdvposDtoPage,"records");
            List<YddAdvpos> records = pageInfo.getRecords();
            List<YddAdvposDto> list = records.stream().map((item) ->{
                YddAdvposDto yddAdvposDto=new YddAdvposDto();
                BeanUtils.copyProperties(item,yddAdvposDto);
                Integer id = item.getId();
                int count = yddAdvimgService.count(id);
                yddAdvposDto.setAdvposnum(count);return yddAdvposDto;
            }).collect(Collectors.toList());
            yddAdvposDtoPage.setRecords(list);
            PageDto<YddAdvposDto> pageDto = new PageDto<>();
            BeanUtils .copyProperties(yddAdvposDtoPage,pageDto);
            pageDto.setValue(yddAdvposDtoPage);
            return pageDto;
    }
}
